﻿using System;
using System.Collections.Generic;
using System.Linq;
using MvcLibrary.Repository;
using System.Web;
using System.IO;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using System.Collections;
using System.Net;




namespace WebVideo.Models
{
    public class LoginUserModel
    {
        [Required(ErrorMessage = "*")]
        [StringLength(12, ErrorMessage = "Username {2}-{1} kí tự", MinimumLength = 3)]
        public string UserName { get; set; }

        [Required(ErrorMessage = "*")]
        [StringLength(20, ErrorMessage = "Password {2}-{1} kí tự", MinimumLength = 6)]
        public string Pass { get; set; }

 
        [Required(AllowEmptyStrings = false, ErrorMessage = "*")]
        [Compare("Pass", ErrorMessage = "*")]
        public string ConfirmPassword { get; set; }

        [Required(ErrorMessage = "*")]
        public string Name { get; set; }

        [Required(ErrorMessage = "*")]
        [EmailAddress(ErrorMessage = "*")]
        public string Email { get; set; }

        [Required(ErrorMessage = "*")]
        [Range(1930, 2013, ErrorMessage = "*")]
        [RegularExpression("^\\+?\\d+$", ErrorMessage = "Sai định dạng")]
        public int? YearBorn { get; set; }

       [Required(ErrorMessage = "*")]
        public string Sex { get; set; }

        [Required(ErrorMessage = "*")]
        public string City { get; set; }

        public string Avatar { get; set; }
        public int? checkdk { get; set; }

        public string BankAccount { get; set; }

        public string Link { get; set; }
        public string UserNameRoot { get; set; }
        public string Ip { get; set; }
        public int? PointVideo { get; set; }
        public int? PointFriend1 { get; set; }
        public int? PointFriend2 { get; set; }
        public int? Status { get; set; }
        public DateTime? Date { get; set; }
        
        [Display(Name = "Captcha", Order = 20)]
        [System.Web.Mvc.Remote("ValidateCaptcha", "Captcha", "", ErrorMessage = "ErrorMessage")]
        [Required(ErrorMessage = "*")]
        public virtual string CaptchaValue { get; set; }

        public static void DangKy(LoginUserModel DangKy)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            Security ser = new Security();

                EntityUser dk = new EntityUser();
                dk.UserName = DangKy.UserName;

                dk.Pass = ser.GetHashPassword(DangKy.Pass);
                dk.Date = DateTime.Now;
                dk.Name = DangKy.Name;
                dk.Email = DangKy.Email;
                dk.YearBorn = DangKy.YearBorn;
                dk.Sex = DangKy.Sex;
                dk.City = DangKy.City;
                dk.Status = 0;
                dk.Link = ser.GetHashLink(DangKy.UserName);
                dk.PointVideo = 0;
                dk.PointFriend1 = 0;
                dk.PointFriend2 = 0;
                dk.Ip = DangKy.Ip;
                db.EntityUsers.InsertOnSubmit(dk);
                db.SubmitChanges();
                if (DangKy.Link != null)
                {
                    // insert vào usernameroot
                    EntityUser us = db.EntityUsers.Where(g => g.Link == DangKy.Link).First();

                    //kiểm tra us.UserName; có đủ 30 người bạn chưa
                    if (db.EntityFriends.Where(g => (g.UserName == us.UserName && g.LevelFriend == 1) || (g.UserNameFriend == us.UserName && g.LevelFriend == 1)).ToList().Count < 30)
                    {
                        dk.UserNameRoot = us.UserName;
                        db.SubmitChanges();
                    }
                   
                }
            
        }
        public static void KH(LoginUserModel khoat, string User)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            Security ser = new Security();

            EntityUser KH = db.EntityUsers.Where(p => p.Link == User).Single();
            KH.Status = 1;
            if (KH.UserNameRoot != null)
            {
                // insert vào usernameroot
                EntityUser us = db.EntityUsers.Where(g => g.UserName == KH.UserNameRoot).First();

                //kiểm tra us.UserName; có đủ 30 người bạn chưa
                if (db.EntityFriends.Where(g => (g.UserName == us.UserName && g.LevelFriend == 1) || (g.UserNameFriend == us.UserName && g.LevelFriend == 1)).ToList().Count < 30)
                {
                 //   KH.UserNameRoot = us.UserName;
                    //insert vào bảng friend
                    EntityFriend f1 = new EntityFriend();
                    f1.UserName = us.UserName;
                    f1.UserNameFriend = KH.UserName;
                    f1.LevelFriend = 1;
                    db.EntityFriends.InsertOnSubmit(f1);
                    db.SubmitChanges();
                    if (us.UserNameRoot != null)
                    {
                        if (db.EntityFriends.Where(g => (g.UserName == us.UserNameRoot && g.LevelFriend == 2) || (g.UserNameFriend == us.UserNameRoot && g.LevelFriend == 2)).ToList().Count < 60)
                        {
                            EntityFriend f2 = new EntityFriend();
                            f2.UserName = us.UserNameRoot;
                            f2.UserNameFriend = KH.UserName;
                            f2.LevelFriend = 2;
                            db.EntityFriends.InsertOnSubmit(f2);
                            db.SubmitChanges();
                        }
                    }
                    db.SubmitChanges();
                }

            }
            db.SubmitChanges();

        }
      


    }
}